import { _decorator, Component, instantiate, Node, Prefab, tween, UITransform } from 'cc';
import { Block } from './Block';
const { ccclass, property } = _decorator;

@ccclass('Layer3')
export class Layer3 extends Component {
    @property([Node])
    slotNodes: Node[] = []
    blockList: Block[] = [];
    @property(Prefab)
    preDelete:Prefab = null;

    resetOrder() {
        for(let i = 0; i < this.blockList.length; i++){
            const block = this.blockList[i];
            block.setTmpPosition(this.slotNodes[i].getPosition());
            if(block.node.getParent() == this.node){
                tween(block.node).to(0.1, { position: block.getTmpPosition() }).start();
            }
        }
    }

    getBlockPosition(block: Block) {
        let tmp:Block[] = [];
        let find:boolean = false;
        let complete:boolean = false;
        for(let ele of this.blockList){
            if(ele.getSpriteFrame().name == block.getSpriteFrame().name){
                find = true;
                tmp.push(ele);
            }else{
                if(find && !complete){
                    tmp.push(block);
                    complete = true;
                }
                tmp.push(ele);
            }
        }
        if(!complete){
            tmp.push(block)
        }
        this.blockList = tmp;
        this.resetOrder();

        return this.node.getComponent(UITransform).convertToWorldSpaceAR(block.getTmpPosition());
    }

    addBlock(block: Block){
        const position = block.node.getWorldPosition();
        const localPosition = this.node.getComponent(UITransform).convertToNodeSpaceAR(position)
        block.setAddLayer3Time();
        block.node.setParent(this.node);
        block.node.setPosition(localPosition);
    }

    delSameBlocks() {
        const tmp:Block[] = [];
        for (let i = 0; i < this.blockList.length; i++){
            if(i === 0){
                tmp.push(this.blockList[i]);
            }else if(tmp[0].getSpriteFrame() == this.blockList[i].getSpriteFrame()){
                tmp.push(this.blockList[i])
            }else{
                tmp.splice(0, tmp.length);
                tmp.push(this.blockList[i])
            }
            if(tmp.length >= 3){
                break;
            }
        }
        if(tmp.length >= 3){
            for(let block of tmp){
                const index = this.blockList.indexOf(block);
                const pre = instantiate(this.preDelete)
                pre.setParent(this.node);
                pre.setPosition(block.node.getPosition());
                tween(pre).delay(0.5).removeSelf().start();
                this.blockList.splice(index, 1);
                block.node.removeFromParent()
                block.origin.node?.removeFromParent();
            }
            this.scheduleOnce(()=> {
                this.resetOrder();
            }, 0.2)
            return true
        }
        return false;
    }
    
    getBlockSize() {
        return this.blockList.length;
    }

    getRemoveBlocks(): Block[] {
        const temp:Block[] = [];
        for(let ele of this.blockList){
            temp.push(ele);
            if(temp.length >= 3) break;
        }
        for(let ele of temp){
            const index = this.blockList.indexOf(ele)
            if(index >= 0){
                this.blockList.splice(index, 1);
                ele.setTmpPosition(ele.node.getWorldPosition())
                ele.node.removeFromParent();
            }
        }
        return temp;
    }

    getLastBlock(): Block {
        let block: Block = null;
        for(let ele of this.blockList){
            if(!block){
                block = ele;
            }
            if(ele.getAddLayer3Time() > block.getAddLayer3Time()){
                block = ele;
            }
        }
        if(block){
            const index = this.blockList.indexOf(block);
            if(index>= 0) {
                this.blockList.splice(index, 1);
                block.setTmpPosition(block.node.getWorldPosition());
                block.node.removeFromParent();
            }
        }
        return block;
    }

}


